package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbCustomerCompany;
import cn.stylefeng.guns.modular.cb.mapper.CbCustomerCompanyMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerCompanyParam;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerCompanyResult;
import  cn.stylefeng.guns.modular.cb.service.CbCustomerCompanyService;
import cn.stylefeng.guns.modular.cb.service.WxMsgService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wj
 * @since 2021-01-07
 */
@Service
public class CbCustomerCompanyServiceImpl extends ServiceImpl<CbCustomerCompanyMapper, CbCustomerCompany> implements CbCustomerCompanyService {

    private static Logger logger = LoggerFactory.getLogger(CbCustomerCompanyServiceImpl.class);

    @Autowired
    private UserService userService;

    @Autowired
    private WxMsgService wxMsgService;

    @Override
    public void add(CbCustomerCompanyParam param){
        try{
            LoginUser user = LoginContextHolder.getContext().getUser();

            //添加企业用户角色
            this.userService.setRoles(user.getId(), String.valueOf(6));

            param.setPhone(user.getAccount());//企业用户电话
            param.setStatus(1); //企业审核状态

            //新增企业
            CbCustomerCompany entity = getEntity(param);
            entity.setUserStatus("ENABLE"); //用户状态
            this.save(entity);
            param.setId(entity.getId());
        }catch (Exception e) {
            logger.error("company user add error：", e);
        }
    }

    @Override
    public void delete(CbCustomerCompanyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbCustomerCompanyParam param){
        try{
            CbCustomerCompany oldEntity = getOldEntity(param);
            CbCustomerCompany newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }catch (Exception e){
            logger.error("update error: ", e);
        }
    }

    @Override
    public CbCustomerCompanyResult findBySpec(CbCustomerCompanyParam param){
        return null;
    }

    @Override
    public List<CbCustomerCompanyResult> findListBySpec(CbCustomerCompanyParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbCustomerCompanyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }



    /**
     * 获取用户企业
     * @param param
     * @return
     */
    @Override
    public CbCustomerCompanyResult getCompany(CbCustomerCompanyParam param) {
        return this.baseMapper.getCompany(param);
    }

    /**
     * 企业数量统计
     * @return
     */
    @Override
    public List<Map<String,Object>> queryDateCount(String startDate, String[] countArr){
        return this.baseMapper.queryDateCount(startDate, countArr);
    }

    /**
     * 查询分享人，及分享数量
     * @return
     */
    @Override
    public LayuiPageInfo referessList(CbCustomerCompanyParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.referessList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CbCustomerCompanyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbCustomerCompany getOldEntity(CbCustomerCompanyParam param) {
        return this.getById(getKey(param));
    }

    private CbCustomerCompany getEntity(CbCustomerCompanyParam param) {
        CbCustomerCompany entity = new CbCustomerCompany();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
